package cc.wfu.domain.order.service.rule.chain;


import cc.common.types.entity.ProductActivityEntity;
import cc.common.types.valobj.ProductStateVO;
import cc.wfu.domain.order.model.entity.ProductActivitySkuEntity;
import cc.wfu.domain.order.service.rule.AbstractActionChain;
import cc.wfu.types.common.Constants;
import cc.wfu.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 活动基础规则过滤【日期、状态】
 * @Date: 2024-04-23-15:02
 */
@Slf4j
@Component(value = "activity_base_action")
public class ActivityBaseActionChain extends AbstractActionChain {
    /**
     * 下单基础过滤
     * @param activitySkuEntity sku订单实体
     * @param activityEntity 活动实体
     * @return 过滤结果
     */
    @Override
    public boolean action(ProductActivitySkuEntity activitySkuEntity, ProductActivityEntity activityEntity) {
        log.info("活动责任链-基础信息【有效期、状态、库存(sku)】校验开始。sku:{} productId:{}", activitySkuEntity.getSku(), activityEntity.getProductId());
        // 校验；活动状态
        if (!ProductStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(Constants.ResponseCode.ACTIVITY_STATE_ERROR.getCode(), Constants.ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        // 校验；活动日期「开始时间 <- 当前时间 -> 结束时间」
        Date currentDate = new Date();
        if (activityEntity.getBeginDateTime().after(currentDate) || activityEntity.getEndDateTime().before(currentDate)) {
            throw new AppException(Constants.ResponseCode.ACTIVITY_DATE_ERROR.getCode(), Constants.ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        // 校验；活动sku库存 「剩余库存从缓存获取的」
        if (activitySkuEntity.getStockCountSurplus() <= 0) {
            throw new AppException(Constants.ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), Constants.ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }

        return next().action(activitySkuEntity, activityEntity);
    }
}
